import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import FormModal from '#cpt/temp/FormModal'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
const FinishModal 	= $Lazy.state(()=>import('./cpt/finish-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '标本条码',		name: 'spec_code' },
			{ label: '项目代码',		name: 'kind_code'},
			{ label: '状态',		name: 'status', 	type:'select', 	loading:true },
			// 增加导出和姓名、医院的查询条件 2021-9-27  --马婷
			{ label: '姓名',		name: 'patient_name'	},
			{ label: '医院名称',		name: 'hosp_id',			type:'select',		loading:true },
		],
		// 提交表单列表
		formData : [
			{ label: '说明',     name: 'explanation', 	full:true, 	type:'textarea' }
		]
	}
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}

	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 状态
			const specState = $fn.setRow(forms, 'status', await f.treatment_state )
			// 医院
			$fn.setRow( forms, 'hosp_id',  await f.getCache({ api:'hospital' }))
			
			this.setState({ forms,specState })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'sp-abnormal-spec/index')
	
	// 表单
	cols = [
		{ title: '标本条码', 	dataIndex: 'spec_code', 		...$fn.table.spec_code },
		{ title: '项目名称', 	dataIndex: 'kind_name', 		width:200 },
		{ title: '项目代码', 	dataIndex: 'kind_code', 		...$fn.table.exp_num },
		{ title: '医院名称', 	dataIndex: 'hosp_name', 		width:200 },
		{ title: '病人姓名', 	dataIndex: 'patient_name',  	...$fn.table.name},
		{ title: '性别', 		dataIndex: 'sex_name', 			...$fn.table.sex },
		{ title: '年龄', 		dataIndex: 'age', 				...$fn.table.age },
		{ title: '发起人', 		dataIndex: 'create_user',  		...$fn.table.name},
		{ title: '状态', 		dataIndex: 'status', 			...$fn.table.status,		render: text => $fn.getState(this.state.specState,text) },
		{ title: '异常时间', 	dataIndex: 'abnormal_time',  	...$fn.table.time},
		{ title: '异常原因', 	dataIndex: 'reason',  			width:230},
		{ title: '异常说明', 	dataIndex: 'explanation',  		width:230},
		TableControls.controls.call(this,{ width: 80 }),
	]
	
	// 表格操作按钮
	table_controls = (text,{uuid, status}) => {
		return [
			+status === 0 ? 
			TableControls.click.call(this, '审核', ()=>{	// 审核
				this.submit ={ uuid }
				this.modal.open({formRef: this.formRef})
			} ):null,
			+status===1?TableControls.click.call(this,'完成',()=>{this.finishModal.open(uuid)}):null	// 完成
		]
	}
	
	// 操作按钮
	controls = () => {
		return [
			Controls.exports.call(this,'sp-abnormal-spec/index',{label:'导出'})	// 导出
		]
	}
	
	render(){
		return (
			<>
				<Page title='标本异常管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { type: -1, isInit: window.isInit} ) }
				</Page>
				{/* 审核 */}
				{FormModal.call(this,{ title:'操作', labelWidth:'s3', width:600, api:'sp-abnormal-spec/audit', })}
				{/* 完成 */}
				<FinishModal onRef = { ref => this.finishModal = ref } onRefresh={()=>this.fetch()} />
			</>
		)
	}
}

export default Index